#include<stdio.h>
#include<arpa/inet.h>   ////大端转小端
#include<sys/socket.h>
#include<netinet/in.h>  ////socket网络编程结构体在定义
#include<stdlib.h>  //atoi
#include "StdTcp.h"
#include<unistd.h> //close 函数


struct TcpServer
{
    int sock;
};
TcpS *InitTcpServer(const char *ip, short int port)
{
    TcpS *s =(TcpS *)malloc(sizeof(TcpS));
    if(s == NULL)
    {
        printf("InitTcpServer malloc error!\n");
        return NULL;
    }

    s->sock = socket(AF_INET,SOCK_STREAM,0);
    if(s->sock < 0)
    {
        perror("socket");
        free(s);
        return  NULL;
    }

    int on = 1;
    if(setsockopt(s->sock,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(int)) <0)
    {
        perror("setsockopt");
        free(s);
        return NULL;
    }

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = inet_addr(ip);

    if(bind(s->sock,(struct sockaddr *)&addr,sizeof(addr)) != 0)
    {
        perror("bind");
        free(s);
        return NULL;
    }

    if(listen(s->sock,20) < 0)
    {
        perror("listen");
        free(s);
        return NULL;
    }

    return s;
}

int TcpServerAccept(TcpS *s)
{
    int acceptSock = 0;

    struct sockaddr_in addr;
    socklen_t len = 1;
    if((acceptSock = accept(s->sock,(struct sockaddr *)&addr,&len)) < 0)
    {
        perror("accept");
        return -1;
    }

    return acceptSock;
}

void TcpServerSend(int ClientSock, void *ptr, size_t size)
{
    if(send(ClientSock,ptr,size,0) < 0)
    {
        perror("send");
    }
}

void TcpServerRecv(int ClientSock, void *ptr, size_t size)
{
    if(recv(ClientSock,ptr,size,0) < 0)
    {
        perror("recv");
    }
}

void ClearTcpServer(TcpS *s)
{
    close(s->sock);
    free(s);
}





struct TcpClient
{
    int sock;
};


TcpC *InitTcpClient(const char *ServerIp, short int ServerPort)
{
    TcpC * c =(TcpC *)malloc(sizeof(TcpC));
    if(c == NULL)
    {
        printf("InitTcpClient malloc error!\n");
        return NULL;
    }

    c->sock = socket(AF_INET,SOCK_STREAM,0);
    
    if(c->sock < 0)
    {
        perror("socket");
        free(c);
        return NULL;
    }
    
    struct sockaddr_in ServerAddr;
    ServerAddr.sin_family = AF_INET;
    ServerAddr.sin_port = htons(ServerPort);
    ServerAddr.sin_addr.s_addr = inet_addr(ServerIp);
    if(connect(c->sock,(struct sockaddr *)&ServerAddr,sizeof(ServerAddr)) < 0)
    {
        perror("connect");
        free(c);
        return NULL;
    }

    return c;
}

void TcpClientSend(TcpC *c, void *ptr, size_t size)
{
    if(send(c->sock,ptr,size,0) < 0 )
    {
        perror("send");
    }
}

void TcpClientRecv(TcpC *c, void *ptr, size_t size)
{
    if(recv(c->sock,ptr,size,0) < 0 )
    {
        perror("recv");
    }
}

void ClearTcpClient(TcpC *c)
{
    close(c->sock);
    free(c);
}